*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure B02	 								   *
********************************************************************************

cap log close                             
log using ${log}/FigureB02.log, replace    

* use EUE data 
use "$data/FTp_u__finaldata.dta", clear

* winsorizing 'coworker wage changes' variable
cap drop actual_ln_mover_change ebayes_delta_ln_mover_change
winsor mean_delta_ln_wage_mover_2015, gen(actual_ln_mover_change) p(0.02)
winsor ebayes_delta_ln_wage_mover_2015, gen(ebayes_delta_ln_mover_change) p(0.02)
					
* variables used for heterogeneity split
global hetlist tenure turnover yearsedu age female
global types ols ml eb 
global cuts 0 1
local num : list sizeof global(hetlist)

*** looping over heterogeneity variables
foreach hetvar in $hetlist {
	
	* axis titles and file prefixes
	if "`hetvar'"=="tenure" {
		local hetname "Tenure"
		local hetsuffix "tenure"
	}
	if "`hetvar'"=="turnover" {
		local hetname "Turnover"
		local hetsuffix "turnover"
	}
	if "`hetvar'"=="yearsedu" {
		local hetname "Years of Education"
		local hetsuffix "yearsedu"
	}
	if "`hetvar'"=="age" {
		local hetname "Age"
		local hetsuffix "age"
	}	
	if "`hetvar'"=="female" {
		local hetname "Female"
		local hetsuffix "sex"
	}
	
	foreach type in $types {
		if "`type'"=="ols" local yvar "actual_ln_mover_change" 
		if "`type'"=="eb" local yvar "ebayes_delta_ln_mover_change"
		if "`type'"=="ml" local yvar "p_delta_ln_wage_mover"

		* separating into above/below median of the heterogeneity variable
		cap drop above_med
		quietly su `hetvar' if ln_own_change_w!=. & `yvar'!=., d
		if "`hetvar'"=="female" { 
			gen above_med = `hetvar'==1
			replace above_med = . if `hetvar'==.
		}
		else{
			gen above_med = `hetvar'>=r(p50)		
		}
			
		* estimating models above / below median 
		foreach cut in $cuts {  
			reg ln_own_change_w `yvar' if above_med==`cut', cluster(pid)
				* save for plot
				matrix regtab=r(table)
				local cil_`cut'_`type'_`hetvar' = string(round(regtab[5,1],0.001),"%4.3f")
				local cih_`cut'_`type'_`hetvar' = string(round(regtab[6,1],0.001),"%4.3f")
				local bet_`cut'_`type'_`hetvar' = string(round(_b[`yvar'],0.001),"%4.3f")
			}


		}


}

* create matrices 
foreach type in $types{
	foreach cut in $cuts{
		matrix beta_`cut'_`type' = J(1,`num',.)
		matrix rownames beta_`cut'_`type' = Estimates
		matrix CI_`cut'_`type' = J(2,`num',.)
		matrix rownames CI_`cut'_`type' = ll95 ul95
	}
}

* store information in matrices
foreach type in $types{
	foreach cut in $cuts{
		local i 0
		foreach hetvar in $hetlist {
			local ++ i	
				matrix beta_`cut'_`type'[1, `i'] = `bet_`cut'_`type'_`hetvar''
				matrix CI_`cut'_`type'[1, `i'] = `cil_`cut'_`type'_`hetvar''
				matrix CI_`cut'_`type'[2, `i'] = `cih_`cut'_`type'_`hetvar''
				matlist CI_`cut'_`type'
				matlist beta_`cut'_`type'
		}
	}
}

* create plot 
coefplot (matrix(beta_0_ols), ci(CI_0_ols) ciopts(lc(sea) lw(0.3)) mcolor(sea) m(S) msize(0.75) label(Unadjusted, Below Median)) ///
         (matrix(beta_0_eb), ci(CI_0_eb) ciopts(lc(midblue) lw(0.3)) mcolor(midblue) m(T) msize(0.75) label(Empirical Bayes, ))  /// 
        (matrix(beta_0_ml), ci(CI_0_ml) ciopts(lc(gs7) lw(0.3)) mcolor(gs7) m(D) msize(0.75) label(ML Benchmark, ))  ///		 
		 (matrix(beta_1_ols), ci(CI_1_ols) ciopts(lc(sea%60) lw(0.3)) mcolor(sea) m(Sh) msize(0.75) label(Unadjusted, Above Median)  ) ///
		 (matrix(beta_1_eb), ci(CI_1_eb) ciopts(lc(midblue%60 lw(0.3)) lw(0.3)) mcolor(midblue%40) m(Th) msize(0.75) label(Empirical Bayes, )  ) ///
        (matrix(beta_1_ml), ci(CI_1_ml) ciopts(lc(gs7) lw(0.3)) mcolor(gs7%60) m(Dh) msize(0.75) label(ML Benchmark, ))  ///		 
         , xline(1, lp(solid) lc(gs6)) xline(0, lp(dash) lc(gs6))  yscale(r(0.2 5.7)  lstyle(none)) xscale(r(-0.25 1)) xlab(, labsize(3.5)) ///
		 legend(order(- "Below Median:" 2 "Unadjusted" 4 "Empirical Bayes" 6 "ML Benchmark" - "" - "Above Median:" 8 "Unadjusted" 10 "Empirical Bayes" 12 "ML Benchmark") cols(1) ring(0) pos(5) size(3.5) bmargin(tiny) region(color(white%0))) ///
		 ylabel( 1 "Tenure" 2 "Turnover" 3 "Education" 4 "Age" 5 "Female", labsize(3.5)) xlabel(-0.2 "-.2" 0 "0" 0.2 ".2" 0.4 ".4" 0.6 ".6" 0.8 ".8" 1 "1", nogrid) xtitle("Slope Coefficients: Belief vs. Benchmark")
		 addplot: scatteri 1.5 0.975 "Accurate Beliefs: Slope = 1", msymbol(i) mlabpos(0) mlabcolor(black) mlabangle(90) legend(order(- "Below Median:" 2 "Unadjusted" 4 "Empirical Bayes" 6 "ML Benchmark" - "" - "Above Median:" 8 "Unadjusted" 10 "Empirical Bayes" 12 "ML Benchmark") cols(1) ring(0) pos(5) size(3.5) bmargin(tiny) region(color(white%0))) yscale(r(0.2 5.7) lstyle(none)) xscale(r(-0.25 1)) xlabel(-0.2 "-.2" 0 "0" 0.2 ".2" 0.4 ".4" 0.6 ".6" 0.8 ".8" 1 "1", nogrid) 

graph display ,	xsize(30) ysize(17.5)
graph export "$log/FigureB02.pdf", replace

log close 
clear
